import { submit, allList } from './activityDetailServ'
// 弹出消息框
import { message } from 'antd'
import { parse } from 'qs'
import { download } from 'utils/util'
/* isEmpty 对象是否为空
 * cloneDeep 对象深拷贝
 */
import {  cloneDeep } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const state = {
    detail: {
        activityRule: '',
        topImgUrl:'',
        posterImgUrl1:'',
        posterImgUrl2:'',
        posterImgUrl3:'',
     },
    num: 3,
}

const tmpModule = {
    // namespace: 'infoAddOrEditModel',
    state: cloneDeep(state),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    dispatch({ type: 'allList', payload: {} });
                }
            })
        },
    },
    effects: {
        //获取列表信息
        * allList({ payload }, { put, call, select }) {
            console.log("1111111");
            try {
                //admin/coupons/config/list
                const { data, success } = yield call(allList, {});
                if (success) {
                    console.log("1111111",success,data);
                    if (data) {
                        yield put({ type: 'updateStore', payload: { detail: data } });
                    }
                }
            } catch (e) { }
        },
        // 删除
        * updateModel({ payload }, { put, select }) {
            message.success('删除成功')
            yield put({ type: 'updateStore', payload });
        },
        * updatenum({ payload }, { put, select }) {
            yield put({ type: 'updateStore', payload });
        },

        // 提交
        * submit({ payload }, { put, call, select }) {
            console.log("submitsubmit",payload);
            if(payload.detail.topImgUrl=="" || payload.detail.posterImgUrl1=="" ||payload.detail.posterImgUrl2=="" ||payload.detail.posterImgUrl3=="" ||payload.detail.activityRule=="" ){
                return message.error('请将信息补充完整');
            }
            try {
                const { data, success } = yield call(submit, payload.detail);
                if (success) {
                    message.success('提交成功')
                }
            } catch (e) { }
        },
        
    },

    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return {...preState, ...action.payload };
        }
    }
}

export default tmpModule